//
//  CoverFlowLayout.swift
//  UICollectionViewDemo
//
//  Created by meidaeriOS on 2017/6/23.
//  Copyright © 2017年 meidaer. All rights reserved.
//

import UIKit

class CoverFlowLayout: UICollectionViewFlowLayout {
    
    override func prepare() {
        super.prepare()
//        print(#function)
//        let margin: CGFloat = 8
//        let collH: CGFloat = 200
//        let itemH = collH - margin * 2
//        let itemW = (UIScreen.main.bounds.width - margin * 5) / 4
//        itemSize = CGSize(width: itemW, height: itemH);
//        minimumLineSpacing = margin
//        minimumInteritemSpacing = margin
//        sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin)
//        scrollDirection = .horizontal
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
//        print(#function)
        let attributes = super.layoutAttributesForElements(in: rect)
        
        let centerX = collectionView!.contentOffset.x + collectionView!.bounds.width / 2
        attributes!.forEach({ (attr) in
            let pad = abs(centerX - attr.center.x)
            let scale = 1.8 - pad / collectionView!.bounds.width
            attr.transform = CGAffineTransform(scaleX: scale, y: scale)
        })
        
//        var moveDistance: CGFloat = CGFloat(MAXFLOAT)
//        var attribute: UICollectionViewLayoutAttributes?
//        for (index, attr) in attributes!.enumerated() {
//            if abs(attr.center.x - centerX) < abs(moveDistance) {
//                moveDistance = attr.center.x - centerX
//                attribute = attr
//            }
//        }
//        print(collectionView!.subviews.count)
//
//        if let teAttribute = attribute {
//            let cell = collectionView!.cellForItem(at: teAttribute.indexPath)
//            if let letCell = cell {
//                collectionView!.bringSubview(toFront: letCell)
//            }
//        }
        return attributes
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        var targetPoint = proposedContentOffset
        
        // 中心点
        let centerX = proposedContentOffset.x + collectionView!.bounds.width
        
        let attributes = self.layoutAttributesForElements(in: CGRect(x: proposedContentOffset.x, y: proposedContentOffset.y, width: collectionView!.bounds.width, height: collectionView!.bounds.height))
        // 需要移动的距离
        var moveDistance: CGFloat = CGFloat(MAXFLOAT)
        
        attributes!.forEach { (attr) in
            if abs(attr.center.x - centerX) < abs(moveDistance) {
                moveDistance = attr.center.x - centerX
            }
        }
        
        print(proposedContentOffset)
        print(moveDistance)
//        if targetPoint.x + moveDistance >= 0 && targetPoint.x + moveDistance <= collectionViewContentSize.width - collectionView!.bounds.width {
//            targetPoint.x += moveDistance
//        }
        
        if targetPoint.x > 0 && targetPoint.x < collectionViewContentSize.width - collectionView!.bounds.width {
            targetPoint.x += moveDistance
        }
        
        return targetPoint
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
//        print("newbunds = \(newBounds)")
        return true
    }
    
    override var collectionViewContentSize: CGSize {        
        return CGSize(width:sectionInset.left + sectionInset.right + (CGFloat(collectionView!.numberOfItems(inSection: 0)) * (itemSize.width + minimumLineSpacing)) - minimumLineSpacing, height: 0)
    }
}
